// subpages0/exchangeVip/exchangeVip.js
var app = getApp();
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    if (!app.globalData.plumSession) {
      console.log("暂未获取到session");
      app.wechatSq(that);
    } else {
      that.requestRemaincfg();
    }
  },
  onShow: function () {

  },
  requestRemaincfg: function () {
    var that = this;
    var data = {};
    data.map = 'applet_community_exchange_cfg';
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            remainCfg: res.data.data
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  toggleCard: function (e) {
    var that = this;
    var curCardinfo = e.currentTarget.dataset.card;
    that.setData({
      curCardinfo: curCardinfo
    })
  },
  confirmExchange: function () {
    var that = this;
    var data = {
      map: 'applet_community_exchange_balance',
      points: that.data.exchangePoint ? that.data.exchangePoint : ''
    };
    if (!data.points) {
      app.errorTip(that, '请输入兑换的积分', 2000);
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确认兑换吗？',
      success(res) {
        if (res.confirm) {
          that.requestExchange(data);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  pointChange:function(e){
    var that = this;
    that.setData({
      exchangePoint:e.detail.value
    })
  },
  requestExchange: function (data) {
    var that = this;
    console.log(data);
    wx.showLoading({
      title: '兑换中',
    })
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                that.requestRemaincfg();
                that.setData({
                  exchangePoint: ''
                })
                wx.navigateTo({
                  url:'/subpages/myBank/myBank'
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})